//
//  UploadController.m
//  A段项目
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "UploadController.h"
#import "DeleUploadImage.h"
#import  <MBProgressHUD.h>

@interface UploadController () <UITextViewDelegate,DeleteImageDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, retain) UITextView *textView;


@end

@implementation UploadController

{
    BOOL state; // 表示选中状态.
}

- (void)dealloc {
    
    [_imagePath release];
    [_imageView release];
    [_textView release];
    
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar.backItem setTitle:@"取消"];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:0 target:self action:@selector(finishAction)];
    
    [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
    
    [self createtextView];
    
    
    [self addImageViewForView];
    
    [self.navigationItem.rightBarButtonItem release];
    

}

#pragma mark --上传图片

/**点击完成是调用此方法. */
- (void)finishAction {
    
    /**当没有图片时走这个方法. */
    if (state) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"上传图片" message:@"请选择一张图片" preferredStyle:1];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"我知道了" style:0 handler:^(UIAlertAction * _Nonnull action) {
            
            [alert popoverPresentationController];
            
        }];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }else {
        
     MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            
            //在此处写上传的代码.
           
            dispatch_async(dispatch_get_main_queue(), ^{
              
                
               hud.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];
                
                hud.mode = MBProgressHUDModeCustomView;
                
                hud.labelText = @"上传成功";
                
                [hud show:YES];
            
                [hud hide:YES afterDelay:1];
        
                [self.navigationController popViewControllerAnimated:YES];
                
            });
            
        });
        
       
    }
    
}


#pragma mark --textView

/**创建 textView. */
- (void)createtextView {
   
   
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 150) textContainer:nil];
    
    self.textView.delegate = self;
    
    self.textView.text = @"图片描述";
    self.textView.textColor = [UIColor lightGrayColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.textView];
    
    self.textView.contentSize = CGSizeMake(self.view.frame.size.width, 0);

    [_textView release];
    
}


- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    
  self.textView.text = @""; //开始编辑时 清空里面的内容.
    self.textView.textColor = [UIColor blackColor];
    
    
}

#pragma mark --添加 imageView;


- (void)addImageViewForView {
    
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:self.imagePath]];
    
    self.imageView.frame = CGRectMake(10, 220, 120, 120);
    
    self.imageView.backgroundColor = [UIColor grayColor];
    
    self.imageView.userInteractionEnabled = YES;
    
    [self.view addSubview:self.imageView];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    
    [self.imageView addGestureRecognizer:tap];
    
    [tap addTarget:self action:@selector(tapAction:)];  /**添加轻拍手势. */
    
    
    
    [_imageView release];
    [tap release];
    
}

/**点击空白处回收键盘. */

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.textView endEditing:YES];
    
    
}

/**添加或删除照片. */
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
 
    if (!state ) {
        
        
        DeleUploadImage *dele = [[DeleUploadImage alloc] init];
        
        dele.image = self.imageView.image;
       
        dele.delegate = self;  //设置代理删除图片.
        
        [self presentViewController:dele animated:YES completion:^{
            
        }];
        
        [dele release];
        
    }else {
        
        /**跳转到选择照片界面. */
    
        [self selectPictureView];
        
    }
    
}

/**DeleUploadImage 的代理方法 删除图片.*/

- (void)selectPictureView {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.delegate = self;
    
    picker.allowsEditing = YES;
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:nil];
    
    [picker release];
    
}

/**选择照片页面点击完成调用此方法. */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    self.imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
         state = !state;  //当前有无照片的状态取反.
    }];
    
}

/*删除图片. */
- (void)deleteImage:(UIImage *)image {
    
    self.imageView.image = [UIImage imageNamed:@"jia"];
    
    state = 1;
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    
    if (self.imageView.image == nil) {
        
        self.imageView.image = [UIImage imageNamed:@"jia"];
        
    }
    
    
}

- (void)viewDidDisappear:(BOOL)animated {
    
    
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
